package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {

    @Autowired
    private EmpService empService;

    @Override
    public PageResult<Clazz> listClazz(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Clazz> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null && name.length() > 0, Clazz::getName, name)
                .between(begin != null && end != null, Clazz::getBeginDate, begin, end);
        Page<Clazz> page1 = this.page(p, lqw);
        List<Clazz> clazzs = page1.getRecords();
        //设置班级状态
        for (Clazz clazz : clazzs) {
            LocalDate endDate = clazz.getEndDate();
            LocalDate beginDate = clazz.getBeginDate();
            if (endDate != null && endDate.isBefore(LocalDate.now())) {
                clazz.setStatus("已结课");
            } else if (beginDate != null && beginDate.isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            } else {
                clazz.setStatus("在读");
            }
        }
        //查找班主任id
        Set<Integer> masterId = clazzs.stream().map(Clazz::getMasterId).filter(Objects::nonNull).collect(Collectors.toSet());
        //判断集合是否为空
        if (masterId != null && masterId.size() > 0) {
            List<Emp> emps = empService.listByIds(masterId);
            Map<Integer, String> collect = emps.stream().filter(Objects::nonNull).collect(Collectors.toMap(Emp::getId, Emp::getName));
            for (Clazz clazz : clazzs) {
                clazz.setMasterName(collect.get(clazz.getMasterId()));
            }
        }
        PageResult<Clazz> pageResult = new PageResult<>(page1.getTotal(), page1.getRecords());
        return pageResult;
    }

    @Override
    public Clazz getByClazzId(Integer id) {
        Clazz clazz = this.getById(id);
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.updateById(clazz);

    }

    @Override
    public void deleteByClazzId(Integer id) {
        this.removeById( id);
    }

    @Override
    public List<Clazz> listAll() {
        List<Clazz> list = this.list();
        return list;
    }

    @Override
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        this.save(clazz);
    }

}
